//
//  ShellInvitationView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/4.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellInvitationView.h"
#import "ShellFriendViewModel.h"

@interface ShellInvitationView()

@property (nonatomic,strong) ShellFriendViewModel *viewModel;

@end

@implementation ShellInvitationView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = (ShellFriendViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    //内容
    UIView *contentView = [[UIView alloc] init];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.and.right.equalTo(scrollView).with.insets(UIEdgeInsetsZero);
        make.width.equalTo(scrollView);
    }];
    
    
    UIImageView *invitationView = [[UIImageView alloc] init];
    [contentView addSubview:invitationView];
    [invitationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(230);
        make.height.mas_equalTo(369);
    }];

    
    ///!!!:推荐码
    UILabel *inviteCodeLab = [UILabel lz_labelWithText:LZLocalizedString(@"推荐码") fontSize:k_textFont(18) color:UIColorHex(0xFFFFFF)];
    [inviteCodeLab lz_centerAlignment];
    [contentView addSubview:inviteCodeLab];
    [inviteCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(invitationView.mas_bottom).mas_offset(30);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(25);
    }];
    
    


    //保存图片
    UIButton *saveBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"保存为图片") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    saveBtn.cornerRadius = 6;
    [saveBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [contentView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(inviteCodeLab.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(230);
        make.height.mas_equalTo(40);
    }];

    //复制链接
    UIButton *copyBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"复制邀请链接") titleColor:UIColorHex(0xFFFFFF) fontSize:16];
    [contentView addSubview:copyBtn];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(saveBtn.mas_bottom).mas_offset(15);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(22);
    }];
    

    //邀请节点的文案
    UILabel *tipLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(13) color:UIColorHex(0x678DA8)];
    CGFloat tipHeight = [tipLabel.text lz_textHeightWithFontSize:tipLabel.font withMaxWidth:K_SCREENWIDTH - 56];
    [contentView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.mas_equalTo(copyBtn.mas_bottom).mas_offset(22);
        make.right.mas_equalTo(-28);
        make.height.mas_equalTo(tipHeight);
    }];
    
    
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(tipLabel.mas_bottom).mas_offset(34);
    }];
    
    
    ///保存图片
    @weakify(self);
    [[saveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [LZ_BoolJudge lz_accessPrivacyPermissionWithType:LZ_PermissionTypeAlbum completion:^(BOOL response) {
            if (response) {
                //使用模态呈现相册
                dispatch_async(dispatch_get_main_queue(), ^{
                    @strongify(self);
                    UIView *invitationView = [self downloadInvitationView];
                    [self loadImageFinished:[invitationView lz_screenshot]];
                });
            }
        }];

    }];

    ///复制链接
    [[copyBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
        pastboard.string = self.viewModel.invitationModel.inviteUrl;
        showDelayedDismissTitle(LZLocalizedString(@"复制成功"), self);
    }];
    

    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
       
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           @strongify(self);


           UIView *invitaView = [self createInvitationView];
             UIImage *invitationImg = [invitaView lz_screenshot];
             invitationView.image = invitationImg;
             inviteCodeLab.text = [NSString stringWithFormat:@"%@: %@",LZLocalizedString(@"推荐码"),self.viewModel.invitationModel.myinviteCode];

             tipLabel.text = self.viewModel.invitationModel.descText;
             CGFloat tipHeight = [tipLabel.text lz_textHeightWithFontSize:tipLabel.font withMaxWidth:K_SCREENWIDTH - 56];
             [contentView addSubview:tipLabel];
             [tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                 make.height.mas_equalTo(tipHeight);
             }];
       
       });
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self);
        ///获取邀请节点的信息
         [self.viewModel getInviteInfo];
    });
 
}


#pragma mark - 保存图片
- (void)loadImageFinished:(UIImage *)image
{
    if(!image)return;
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(error){
        showDelayedDismissTitle(LZLocalizedString(@"保存失败"), nil);
    } else {
        showDelayedDismissTitle(LZLocalizedString(@"保存成功"), nil);
    }
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}



#pragma mark - get && set
- (UIView *)createInvitationView{
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 600)];
    UIImageView *invitationView = [[UIImageView alloc]init];
    UIImage *gradientImg = [UIImage gradientImageWithColors:@[UIColorHex(0x210145),UIColorHex(0x3C006B),UIColorHex(0x021528)] rect:CGRectMake(0, 0,375,600) direction:LZGradientImageDirectionTopToBottom];
    [contentView addSubview:invitationView];
    [invitationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(375);
        make.height.mas_equalTo(600);
    }];
    invitationView.image = gradientImg;
    
    UILabel *remaekLab1 = [UILabel lz_labelWithText:LZLocalizedString(@"国际贸易区块清算系统") fontSize:k_textBoldFont(37) color:UIColorHex(0xFFFFFF)];
    remaekLab1.adjustsFontSizeToFitWidth = YES;
    [remaekLab1 lz_centerAlignment];
    [invitationView addSubview:remaekLab1];
    [remaekLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(42);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *remaekLab2 = [UILabel lz_labelWithText:LZLocalizedString(@"招募全球经纪人") fontSize:k_textBoldFont(31) color:UIColorHex(0xFFFFFF)];
    remaekLab2.adjustsFontSizeToFitWidth = YES;
    [remaekLab2 lz_centerAlignment];
    [invitationView addSubview:remaekLab2];
    [remaekLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(remaekLab1.mas_bottom).mas_offset(-4);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *remaekLab3 = [UILabel lz_labelWithText:LZLocalizedString(@"颠覆传统 轻松赚钱") fontSize:k_textFont(18) color:UIColorHex(0xFFFFFF)];
    remaekLab3.adjustsFontSizeToFitWidth = YES;
    [remaekLab3 lz_centerAlignment];
    [invitationView addSubview:remaekLab3];
    [remaekLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(remaekLab2.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(25);
    }];

    
    UIImageView *bgImgView = [[UIImageView  alloc] init];
    bgImgView.image = k_imageName(@"icon_invitation_bg");
    [invitationView addSubview:bgImgView];
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.width.mas_equalTo(321);
        make.height.mas_equalTo(345);
        make.top.mas_equalTo(166);
    }];
    
    
    
    UIView *invitaContentView = [[UIView  alloc] init];
    invitaContentView.backgroundColor = UIColor.whiteColor;
    [invitationView addSubview:invitaContentView];
    [invitaContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(375);
        make.height.mas_equalTo(120);
    }];
    
    
    
    UILabel *remaekLab4 = [UILabel lz_labelWithText:LZLocalizedString(@"Shell，轻松赚取交易佣金") fontSize:k_textFont(18) color:UIColorHex(0x04172A)];
    remaekLab4.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab4];
    [remaekLab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(25);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(25);
    }];
    
    
    UILabel *remaekLab5 = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    remaekLab5.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab5];
    [remaekLab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remaekLab4.mas_bottom).mas_offset(3);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(28);
    }];
    
    NSString *inviteCode = [NSString stringWithFormat:@"%@：%@",LZLocalizedString(@"注册时请填写邀请码"),self.viewModel.invitationModel.myinviteCode];
    NSMutableAttributedString *inviteCodeAttributedString = [NSAttributedString attributedString:inviteCode rangeTitle:self.viewModel.invitationModel.myinviteCode leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0x8D99B2) rightColor:COLOR_TINT];
    remaekLab5.attributedText = inviteCodeAttributedString;
    
    
    UILabel *remaekLab6 = [UILabel lz_labelWithText:LZLocalizedString(@"长按识别二维码") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    remaekLab6.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab6];
    [remaekLab6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remaekLab5.mas_bottom).mas_offset(3);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(17);
    }];
       
    UIImageView *scanImageView = [[UIImageView  alloc] init];
    scanImageView.backgroundColor = UIColorHex(0xEFF0FC);
    [invitaContentView addSubview:scanImageView];
    [scanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(invitaContentView.mas_centerY);
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(86);
        make.height.mas_equalTo(86);
    }];
    
    UIImageView *iconImageView = [[UIImageView  alloc] init];
    iconImageView.cornerRadius = 6;
    [scanImageView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(scanImageView.mas_centerX);
        make.centerY.mas_equalTo(scanImageView.mas_centerY);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    UIImage *scanImg = [UIImage creatNonInterContentString:self.viewModel.invitationModel.inviteUrl withSize:86];
    scanImageView.image = scanImg;
    iconImageView.image = k_imageName(@"iconiApp");
    
    return contentView;
}


//保存到本地
- (UIView *)downloadInvitationView{
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 812)];
    UIImageView *invitationView = [[UIImageView alloc]init];
    UIImage *gradientImg = [UIImage gradientImageWithColors:@[UIColorHex(0x210145),UIColorHex(0x3C006B),UIColorHex(0x021528)] rect:CGRectMake(0, 0,375,812) direction:LZGradientImageDirectionTopToBottom];
    [contentView addSubview:invitationView];
    [invitationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(375);
        make.height.mas_equalTo(812);
    }];
    invitationView.image = gradientImg;
    
    UILabel *remaekLab1 = [UILabel lz_labelWithText:LZLocalizedString(@"国际贸易区块清算系统") fontSize:k_textBoldFont(37) color:UIColorHex(0xFFFFFF)];
    remaekLab1.adjustsFontSizeToFitWidth = YES;
    [remaekLab1 lz_centerAlignment];
    [invitationView addSubview:remaekLab1];
    [remaekLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(54);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *remaekLab2 = [UILabel lz_labelWithText:LZLocalizedString(@"招募全球经纪人") fontSize:k_textBoldFont(31) color:UIColorHex(0xFFFFFF)];
    remaekLab2.adjustsFontSizeToFitWidth = YES;
    [remaekLab2 lz_centerAlignment];
    [invitationView addSubview:remaekLab2];
    [remaekLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(remaekLab1.mas_bottom).mas_offset(-4);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *remaekLab3 = [UILabel lz_labelWithText:LZLocalizedString(@"颠覆传统 轻松赚钱") fontSize:k_textFont(18) color:UIColorHex(0xFFFFFF)];
    remaekLab3.adjustsFontSizeToFitWidth = YES;
    [remaekLab3 lz_centerAlignment];
    [invitationView addSubview:remaekLab3];
    [remaekLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(remaekLab2.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(365);
        make.height.mas_equalTo(25);
    }];

    
    UIImageView *bgImgView = [[UIImageView  alloc] init];
    bgImgView.image = k_imageName(@"icon_invitation_download_bg");
    [invitationView addSubview:bgImgView];
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.width.mas_equalTo(375);
        make.height.mas_equalTo(558);
        make.top.mas_equalTo(186);
    }];
    
    
    
    UIView *invitaContentView = [[UIView  alloc] init];
    invitaContentView.backgroundColor = UIColor.whiteColor;
    [invitationView addSubview:invitaContentView];
    [invitaContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(375);
        make.height.mas_equalTo(120);
    }];
    
    
    
    UILabel *remaekLab4 = [UILabel lz_labelWithText:LZLocalizedString(@"Shell，轻松赚取交易佣金") fontSize:k_textFont(18) color:UIColorHex(0x04172A)];
    remaekLab4.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab4];
    [remaekLab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(17);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(25);
    }];
    
    ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
    
    UILabel *remaekLab5 = [UILabel lz_labelWithText:[NSString stringWithFormat:LZLocalizedString(@"您的好友 %@邀请您加入"),detailModel.email] fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    remaekLab5.numberOfLines = 0;
    remaekLab5.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab5];
    [remaekLab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remaekLab4.mas_bottom).mas_offset(2);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(21);
   }];
    
    
    UILabel *remaekLab6 = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    remaekLab6.adjustsFontSizeToFitWidth = YES;
    remaekLab6.numberOfLines = 0;
    [invitaContentView addSubview:remaekLab6];
    [remaekLab6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remaekLab5.mas_bottom);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(22);
    }];
    
    NSString *inviteCode = [NSString stringWithFormat:@"%@：%@",LZLocalizedString(@"注册时请填写邀请码"),self.viewModel.invitationModel.myinviteCode];
    NSMutableAttributedString *inviteCodeAttributedString = [NSAttributedString attributedString:inviteCode rangeTitle:self.viewModel.invitationModel.myinviteCode leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0x8D99B2) rightColor:COLOR_TINT];
    remaekLab6.attributedText = inviteCodeAttributedString;
    
    
    UILabel *remaekLab7 = [UILabel lz_labelWithText:LZLocalizedString(@"长按识别二维码") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    remaekLab7.adjustsFontSizeToFitWidth = YES;
    [invitaContentView addSubview:remaekLab7];
    [remaekLab7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remaekLab6.mas_bottom).mas_offset(4);
        make.right.mas_equalTo(-110);
        make.height.mas_equalTo(17);
    }];
       
    UIImageView *scanImageView = [[UIImageView  alloc] init];
    scanImageView.backgroundColor = UIColorHex(0xEFF0FC);
    [invitaContentView addSubview:scanImageView];
    [scanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(invitaContentView.mas_centerY);
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(86);
        make.height.mas_equalTo(86);
    }];
    
    UIImageView *iconImageView = [[UIImageView  alloc] init];
    iconImageView.cornerRadius = 6;
    [scanImageView addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(scanImageView.mas_centerX);
        make.centerY.mas_equalTo(scanImageView.mas_centerY);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    UIImage *scanImg = [UIImage creatNonInterContentString:self.viewModel.invitationModel.inviteUrl withSize:86];
    scanImageView.image = scanImg;
    iconImageView.image = k_imageName(@"iconiApp");
    
    return contentView;
}


@end
